﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WanMeiModel;

namespace WanMeiService
{
    /// <summary>
    /// 用户状态信息存储文件
    /// </summary>
    public class PublicDataInfomation
    {
        private PublicDataInfomation() { }
        //定义一个线程安全标识
        private static readonly object locker = new object();
        private static PublicDataInfomation _instance = null;
        public static PublicDataInfomation Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (locker)
                    {
                        if (_instance == null)
                        {
                            _instance = new PublicDataInfomation();
                        }
                    }
                }
                return _instance;
            }
        }
        private List<UserInfoModel> _currentUserList = new List<UserInfoModel>();
        /// <summary>
        /// 当前在线用户信息集合
        /// </summary>
        public List<UserInfoModel> CurrentUserList
        {
            get
            {
                return _currentUserList;
            }
            set
            {
                _currentUserList = value;
            }
        }
        /// <summary>
        /// 当前全部在线用户数量
        /// </summary>
        public int CurrentUserCount
        {
            get
            {
                return _currentUserList.Count;
            }
        }
        /// <summary>
        /// 连接成功，向在线用户集合添加用户
        /// </summary>
        /// <param name="user"></param>
        public bool AddUser(UserInfoModel user)
        {
            var result = false;
            if (!this._currentUserList.Any(p => p.ConnectedId == user.ConnectedId))
            {
                this._currentUserList.Add(user);
                result = true;
            }
            return result;
        }
        /// <summary>
        /// 断开连接移除用户
        /// </summary>
        /// <param name="user"></param>
        public bool RemoveUser(string connectid)
        {
            var result = false;
            if (this._currentUserList.Any(p => p.ConnectedId == connectid))
            {
                this._currentUserList.Remove(GetUser(connectid));
                result = true;
            }
            return result;
        }
        /// <summary>
        /// 根据连接唯一标识获取用户
        /// </summary>
        /// <param name="connectid"></param>
        /// <returns></returns>
        public UserInfoModel GetUser(string connectid)
        {
            return _currentUserList.Where(p => p.ConnectedId == connectid).FirstOrDefault();
        }
        /// <summary>
        /// 根据用户账号获取用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserInfoModel GetUserByUserId(string userName)
        {
            return _currentUserList.Where(p => p.UserName == userName).FirstOrDefault();
        }
    }
}
